// *****************************************************************************
/* Proporciona una tabla que no puede ser modificada directamente */
// *****************************************************************************
package horde.internalResources.HorrorControl.HCvis.components;
// *****************************************************************************
// <editor-fold defaultstate="collapsed" desc="Imports">
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
// </editor-fold>
// *****************************************************************************

/**
 * @author Andres Picazo Cuesta
 */
public class LockedTable extends JTable {

    public LockedTable() {
        super();
    }

    public LockedTable(Object[][] datos, String[] columnas) {
        super(datos, columnas);
    }

    public LockedTable(TableModel t) {
        super(t);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int mColIndex) {
        return false;
    }

    /* Devuelvo el fondo para la fila */
    protected Color colorForRow(int row) {
        return (row % 2 == 0) ? Color.LIGHT_GRAY : getBackground();
    }

    /* Pongo el color de fondo segun la fila seleccionada */
    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        try {
            Component c = super.prepareRenderer(renderer, row, column);
            /* Colores para la fila y columna seleccionadas */
            if (isCellSelected(row, column) == false) {
                c.setBackground(colorForRow(row));
                c.setForeground(UIManager.getColor("Table.foreground"));
            } else { /* Colores para el resto */
                c.setBackground(UIManager.getColor("Table.selectionBackground"));
                c.setForeground(UIManager.getColor("Table.selectionForeground"));
            }
            return c;
        } catch (Exception e) {
            return null;
        }
        
    }

    public void initColumnSizes() {
        try {
            TableColumn column = null;
            Component comp = null;
            int headerWidth = 0;
            int cellWidth = 0;
            int maxCellWidth;
            for (int i = 0; i < getModel().getColumnCount(); i++) {
                column = getColumnModel().getColumn(i);
                maxCellWidth = 0;
                for (int j = 0; j < getModel().getRowCount(); j++) {
                    TableCellRenderer renderer = getTableHeader().getDefaultRenderer();
                    comp = renderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, j, i);
                    headerWidth = comp.getPreferredSize().width;

                    renderer = getDefaultRenderer(getModel().getColumnClass(i));

                    comp = renderer.getTableCellRendererComponent(null, getModel().getValueAt(j, i), false, false, j, i);
                    cellWidth = comp.getPreferredSize().width;
                    maxCellWidth = Math.max(maxCellWidth, Math.max(headerWidth, cellWidth));
                }
                column.setPreferredWidth(maxCellWidth);
            }
        } catch (Exception e) {
        }

    }
}
// *****************************************************************************

